import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import router from '../router'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    idToken: null,
    userId: null,
    user: null,
    isLoginPage:true
  },
  mutations: {
    authUser (state, userData) {
      state.idToken = userData.token
      state.isLoginPage = false
     // state.userId = userData.userId
    },
    storeUser (state, user) {
      state.user = user
    },
    clearAuthData (state) {
      state.idToken = null
      state.userId = null
    }
  },
  actions: {
    setLogoutTimer ({commit}, expirationTime) {
      setTimeout(() => {
        commit('clearAuthData')
      }, expirationTime * 1000)
    },
    checkSignStatus ({commit, dispatch}, authData) {
      if(authData.qrcode){
        axios.post(`/partner/signIn/checkStatus/${authData.qrcode}`)
          .then(res => {
            if(authData.timerId){
              clearTimeout(authData.timerId)
            }
            if(!res.data.success ){
              // authData.timerId = setTimeout(() =>{
              //   dispatch('checkSignStatus', authData)
              // },3000)
              setTimeout(() =>{
                console.log("=== test start ===")  //测试用 之后需要删除
                commit('authUser', {
                  token: '12112'
                })
                router.push("/home") 
                 const now = new Date()
                const expirationDate = new Date(now.getTime() + 180 * 1000)
                localStorage.setItem('token', '12112')
                localStorage.setItem('expirationDate', expirationDate)
                //dispatch('storeUser', authData)
                dispatch('setLogoutTimer', 180)
                console.log("=== test end ===")
              },5000)
             }else{
              clearTimeout(authData.timerId)
              commit('authUser', {
                token: res.data.token
              })
              this.$router.push('/home')
              // const now = new Date()
              // const expirationDate = new Date(now.getTime() + res.data.expiresIn * 1000)
              localStorage.setItem('token', res.data.idToken)
              // localStorage.setItem('userId', res.data.localId)
              // localStorage.setItem('expirationDate', expirationDate)
              // dispatch('storeUser', authData)
              // dispatch('setLogoutTimer', res.data.expiresIn)
             }  
          })
          .catch(error => console.log(error))
      }
    },
    tryAutoLogin ({commit}) {
      const token = localStorage.getItem('token')
      if (!token) {
        return
      }
      const expirationDate = localStorage.getItem('expirationDate')
      const now = new Date()
      if (now >= expirationDate) {
        return
      }
     // const userId = localStorage.getItem('userId')
      commit('authUser', {
        token: token
      })
    },
  },
  getters: {
    user (state) {
      return state.user
    },
    isAuthenticated (state) {
      return state.idToken !== null
    },
    isLoginPage (state) {
      return state.isLoginPage
    }
  },
  modules: {
  }
})
